/** @file Manage all html objects */

/** @var {Element} - load file button */
let file_load = document.getElementById("file-load");
/** @var {Element} - hidden real file input */
let file_load_input = document.getElementById("file-load-input");
/** @var {Element} - is file_load button down */
let file_load_down = false;
/** @var {Element} - save file button */
let file_save = document.getElementById("file-save");
/** @var {Element} - is file_save button down */
let file_save_down = false;

/* ************************************************** */

let extens_list = document.getElementById("extens-list");
let extens_remove = document.getElementById("extens-remove");

/** @var {Element} - debug text field */
let debug_area = document.getElementById("debug-area");

/** @var {Element} - script not loadad tip */
let noname_noscript = document.getElementById("noname-noscript");

/* ************************************************** */

/** @var {Element} - store the sb3 object */
let global_sb3 = null;

/* ************************************************** */

/* load/save */
file_load.onmouseup = () => {
	// Upload a file.
	file_load_input.click();
};

file_load_input.onchange = () => {
	if (file_load_input.files.length > 0) {
		let file = file_load_input.files[0];
		// file.name
		file_load.classList.add("down");
		file_load.classList.add("progress-infinite");
		let sb3 = global_sb3 = new mysb3();
		sb3.loadFile(file)
			.then(() => {
				error("ok");
			})
			.catch(err => {
				error(err.message);
			})
			.finally(() => {
				file_load.classList.remove("down");
				file_load.classList.remove("progress-infinite");
			});
	}
};

file_save.onmouseup = () => {
	if (global_sb3 === null || file_save_down) {
		return;
	}
	file_save_down = true;
	file_save.style.setProperty("--progress", "0%");
	file_save.classList.add("down");
	file_save.classList.add("progress");
	global_sb3.saveFile(9, progress => {
			file_save.style.setProperty("--progress", progress + "%");
		})
		.then(() => {
			return;
		})
		.catch(err => {
			error(err.message);
		})
		.finally(() => {
			file_save_down = false;
			file_save.classList.remove("down");
			file_save.classList.remove("progress");
		});
};

/* Hide "noscript" after script loaded. */
noname_noscript.style.display = "none";

/** remove extensions */
let whiteList = {
	"opcode": [
		"motion_movesteps",
		"motion_turnright",
		"motion_turnleft",
		"motion_goto",
		"motion_goto_menu",
		"motion_gotoxy",
		"motion_glideto",
		"motion_glideto_menu",
		"motion_glidesecstoxy",
		"motion_pointindirection",
		"motion_pointtowards",
		"motion_pointtowards_menu",
		"motion_changexby",
		"motion_setx",
		"motion_changeyby",
		"motion_sety",
		"motion_ifonedgebounce",
		"motion_setrotationstyle",
		"motion_xposition",
		"motion_yposition",
		"motion_direction",
		"looks_sayforsecs",
		"looks_say",
		"looks_thinkforsecs",
		"looks_think",
		"looks_switchcostumeto",
		"looks_costume",
		"looks_nextcostume",
		"looks_switchbackdropto",
		"looks_backdrops",
		"looks_nextbackdrop",
		"looks_changesizeby",
		"looks_setsizeto",
		"looks_changeeffectby",
		"looks_seteffectto",
		"looks_cleargraphiceffects",
		"looks_show",
		"looks_hide",
		"looks_gotofrontback",
		"looks_goforwardbackwardlayers",
		"looks_costumenumbername",
		"looks_backdropnumbername",
		"looks_size",
		"sound_playuntildone",
		"sound_sounds_menu",
		"sound_play",
		"sound_stopallsounds",
		"sound_changeeffectby",
		"sound_seteffectto",
		"sound_cleareffects",
		"sound_changevolumeby",
		"sound_setvolumeto",
		"sound_volume",
		"event_whenflagclicked",
		"event_whenkeypressed",
		"event_whenthisspriteclicked",
		"event_whenbackdropswitchesto",
		"event_whengreaterthan",
		"event_whenbroadcastreceived",
		"event_broadcast",
		"event_broadcastandwait",
		"control_wait",
		"control_repeat",
		"control_forever",
		"control_if",
		"control_if_else",
		"control_wait_until",
		"control_repeat_until",
		"control_stop",
		"control_start_as_clone",
		"control_create_clone_of",
		"control_create_clone_of_menu",
		"control_delete_this_clone",
		"sensing_touchingobject",
		"sensing_touchingobjectmenu",
		"sensing_touchingcolor",
		"sensing_coloristouchingcolor",
		"sensing_distanceto",
		"sensing_distancetomenu",
		"sensing_askandwait",
		"sensing_answer",
		"sensing_keypressed",
		"sensing_keyoptions",
		"sensing_mousedown",
		"sensing_mousex",
		"sensing_mousey",
		"sensing_setdragmode",
		"sensing_loudness",
		"sensing_timer",
		"sensing_resettimer",
		"sensing_of",
		"sensing_of_object_menu",
		"sensing_current",
		"sensing_dayssince2000",
		"sensing_username",
		"operator_add",
		"operator_subtract",
		"operator_multiply",
		"operator_divide",
		"operator_random",
		"operator_gt",
		"operator_lt",
		"operator_equals",
		"operator_and",
		"operator_or",
		"operator_not",
		"operator_join",
		"operator_letter_of",
		"operator_length",
		"operator_contains",
		"operator_mod",
		"operator_round",
		"operator_mathop",
		"data_setvariableto",
		"data_changevariableby",
		"data_showvariable",
		"data_hidevariable",
		"data_addtolist",
		"data_insertatlist",
		"data_deleteoflist",
		"data_deletealloflist",
		"data_replaceitemoflist",
		"data_itemoflist",
		"data_itemnumoflist",
		"data_lengthoflist",
		"data_listcontainsitem",
		"data_showlist",
		"data_hidelist",
		"procedures_definition",
		"procedures_prototype",
		"procedures_call",
		"argument_reporter_string_number",
		"argument_reporter_boolean",
		"music_restForBeats",
		"music_playNoteForBeats",
		"note",
		"music_setInstrument",
		"music_menu_INSTRUMENT",
		"music_setTempo",
		"music_changeTempo",
		"music_getTempo",
		"pen_clear",
		"pen_stamp",
		"pen_penDown",
		"pen_penUp",
		"pen_setPenColorToColor",
		"pen_changePenColorParamBy",
		"pen_menu_colorParam",
		"pen_setPenColorParamTo",
		"pen_changePenSizeBy",
		"pen_setPenSizeTo",
		"videoSensing_whenMotionGreaterThan",
		"videoSensing_videoOn",
		"videoSensing_menu_SUBJECT",
		"videoSensing_menu_ATTRIBUTE",
		"videoSensing_videoToggle",
		"videoSensing_menu_VIDEO_STATE",
		"videoSensing_setVideoTransparency",
		"text2speech_speakAndWait",
		"text2speech_setVoice",
		"text2speech_menu_voices",
		"text2speech_setLanguage",
		"text2speech_menu_languages",
		"translate_getTranslate",
		"translate_menu_languages",
		"translate_getViewerLanguage",
		"makeymakey_whenMakeyKeyPressed",
		"makeymakey_menu_KEY",
		"makeymakey_whenCodePressed",
		"makeymakey_menu_SEQUENCE",
		"microbit_whenButtonPressed",
		"microbit_menu_buttons",
		"microbit_isButtonPressed",
		"microbit_whenGesture",
		"microbit_menu_gestures",
		"microbit_displaySymbol",
		"matrix",
		"microbit_displayText",
		"microbit_displayClear",
		"microbit_whenTilted",
		"microbit_menu_tiltDirectionAny",
		"microbit_isTilted",
		"microbit_getTiltAngle",
		"microbit_menu_tiltDirection",
		"microbit_whenPinConnected",
		"microbit_menu_touchPins",
		"ev3_motorTurnClockwise",
		"ev3_menu_motorPorts",
		"ev3_motorTurnCounterClockwise",
		"ev3_motorSetPower",
		"ev3_getMotorPosition",
		"ev3_whenButtonPressed",
		"ev3_menu_sensorPorts",
		"ev3_whenDistanceLessThan",
		"ev3_whenBrightnessLessThan",
		"ev3_buttonPressed",
		"ev3_getDistance",
		"ev3_getBrightness",
		"ev3_beep",
		"boost_motorOnFor",
		"boost_menu_MOTOR_ID",
		"boost_motorOnForRotation",
		"boost_motorOn",
		"boost_motorOff",
		"boost_setMotorPower",
		"boost_setMotorDirection",
		"boost_menu_MOTOR_DIRECTION",
		"boost_getMotorPosition",
		"boost_menu_MOTOR_REPORTER_ID",
		"boost_whenColor",
		"boost_menu_COLOR",
		"boost_seeingColor",
		"boost_whenTilted",
		"boost_menu_TILT_DIRECTION_ANY",
		"boost_getTiltAngle",
		"boost_menu_TILT_DIRECTION",
		"boost_setLightHue",
		"wedo2_motorOnFor",
		"wedo2_menu_MOTOR_ID",
		"wedo2_motorOn",
		"wedo2_motorOff",
		"wedo2_startMotorPower",
		"wedo2_setMotorDirection",
		"wedo2_menu_MOTOR_DIRECTION",
		"wedo2_setLightHue",
		"wedo2_whenDistance",
		"wedo2_menu_OP",
		"wedo2_whenTilted",
		"wedo2_menu_TILT_DIRECTION_ANY",
		"wedo2_getDistance",
		"wedo2_isTilted",
		"wedo2_getTiltAngle",
		"wedo2_menu_TILT_DIRECTION",
		"gdxfor_whenGesture",
		"gdxfor_menu_gestureOptions",
		"gdxfor_whenForcePushedOrPulled",
		"gdxfor_menu_pushPullOptions",
		"gdxfor_getForce",
		"gdxfor_whenTilted",
		"gdxfor_menu_tiltAnyOptions",
		"gdxfor_isTilted",
		"gdxfor_getTilt",
		"gdxfor_menu_tiltOptions",
		"gdxfor_isFreeFalling",
		"gdxfor_getSpinSpeed",
		"gdxfor_menu_axisOptions",
		"gdxfor_getAcceleration",
		"data_listcontents",
		"data_variable"
	],
	"extension": [
		"music",
		"pen",
		"videoSensing",
		"text2speech",
		"translate",
		"makeymakey",
		"microbit",
		"ev3",
		"boost",
		"wedo2",
		"gdxfor"
	]
}

extens_list.onmouseup = function () {
	try {
		let extens = new extension(global_sb3.project);
		//extens.whiteList = whiteList;
		extens.getWhiteList();
		error(extens.whiteList);
		whiteList = extens.whiteList;
	} catch (e) {
		error(e.message);
	}
};

extens_remove.onmouseup = function () {
	try {
		let extens = new extension(global_sb3.project);
		extens.whiteList = whiteList;
		extens.remove();
		// error(global_sb3.project);
	} catch (e) {
		error(e.message);
	}
};

/** debug */
function error(err) {
	debug_area.value = JSON.stringify(err);
}
